<template>
  <div class="padding flex direction-column height-100">
    <div class="search-form" v-if="false">
      <el-form inline>
        <el-form-item label="能源品种">
          <el-select v-model="category"></el-select>
        </el-form-item>
        <el-form-item label="计量位置">
          <el-input v-model="position"></el-input>
        </el-form-item>
        <el-form-item label="定期提醒">
          <el-select v-model="remind"></el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getLists">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="button-group">
      <el-button type="primary" @click="add">新增</el-button>
    </div>
    <div class="flex-1 overflow-auto dark-table-wrapper">
      <el-table
        class="table" v-loading="loading" :data="lists"
        height="100%"
        :header-cell-style="{
          backgroundColor: themeColor,
          color: 'white'
        }"
      >
        <div slot="empty" class="empty-lists">
          <i class="i-custom:search"></i>
          <span class="empty-text">暂无数据</span>
        </div>
        <el-table-column v-for="column in columns" :key="column.label" v-bind="column"></el-table-column>
        <el-table-column label="操作" prop="operate">
          <template #default="{ row }">
            <td-button title="编辑" @click="edit(row)">
              <i class="i-custom:editor"></i>
            </td-button>
            <td-button title="删除" class="del-btn" icon-style="color: var(--danger-color);border-color: var(--danger-color);" @click="del(row)">
              <i class="i-custom:ashbin"></i>
            </td-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      class="pagination" layout="total,prev,pager,next" background hide-on-single-page
      :current-page.sync="pagination.pageNumber" :page-size="pagination.pageSize"
      :total="pagination.total"
      @current-change="getLists"
    ></el-pagination>
    <operate-modal ref="modal" :modal-title="modalTitle" :modal-data="modalData" @save-success="reload"></operate-modal>
  </div>
</template>

<script>
import TdButton from '@/components/td-button'
import OperateModal from './operate-modal'
import { selectMeasuringToolsList, del } from '@/api/measuringTools'
import { confirmDel } from '@/utils'

const api = {
  selectMeasuringToolsList,
  del
}
export default {
  name: 'measuring-instruments-ledger-index',
  components: {
    TdButton,
    OperateModal
  },
  data () {
    return {
      category: '',
      position: '',
      remind: '',
      loading: false,
      lists: [],
      columns: [
        { label: '计量器具名称', prop: 'toolsName' },
        { label: '运行状态', prop: 'runningState' },
        { label: '计量器具编号', prop: 'toolsNumber' },
        { label: '能源品种', prop: 'energyVariety' },
        { label: '计量等级', prop: 'measurementLevel' },
        { label: '型号规格', prop: 'typeSpecifications' },
        { label: '生产厂家', prop: 'manufacturer' },
        { label: '计量位置', prop: 'installationLocation' },
        { label: '检定周期(天)', prop: 'testCycle' },
        { label: '下次检定时间', prop: 'nextTestTime' },
      ],
      pagination: {
        pageNumber: 1,
        pageSize: 12,
        total: 0
      },
      modalTitle: '',
      modalData: {},
    }
  },
  computed: {
    themeColor () {
      return this.$store.state.user.theme
    }
  },
  mounted () {
    this.getLists()
  },
  methods: {
    reload () {
      this.getLists()
    },
    getLists () {
      this.loading = true
      return api.selectMeasuringToolsList().then(res => {
        if (res.code === 200) {
          this.lists = res.data
        }
      }).finally(() => {
        this.loading = false
      })
    },
    add () {
      this.modalTitle = '新增信息'
      this.modalData = {}
      this.$nextTick(() => {
        this.$refs.modal.open()
      })
    },
    edit (row) {
      this.modalTitle = '编辑信息'
      this.modalData = { ...row }
      this.$nextTick(() => {
        this.$refs.modal.open()
      })
    },
    del (row) {
      confirmDel(this).then(() => {
        api.del(row.id).then(res => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.reload()
          }
        })
      }).catch(() => {})
    },
  },
}
</script>
